/** \file    profile/cmdline.cpp
  * \brief   Stores parsed command line and gives easy access to all the options.
  * \date    2003
  * \author  Fedor Chelnokov
  * \version $Header: /home/cscom1r4/cvsroot/cs/profile/src/cmdline.cpp,v 1.3 2004/02/11 17:47:39 cscom1r4 Exp $
  */

#include "profile/cmdline.h"

void cmdline::reset(int argc, const char ** argv)
{
	options.clear();
	args.clear();
	for (int i = 1; i < argc; ++i) {
		std::string s = argv[i];
		if (s[0] == '-') {
			s.erase(0,1);
			options.insert(s);
		}
		else
			args.push_back(s);
	}
}

bool cmdline::has_option(const std::string & o) const
{
	return options.find(o) != options.end();
}

std::string cmdline::option_suffix(const std::string & prefix) const
{
	options_type::const_iterator i = options.lower_bound(prefix);
	if (i == options.end() || i->compare(0, prefix.size(), prefix) != 0)
		return std::string();
	return i->substr(prefix.size());
}

std::string cmdline::option_suffix(const std::string & prefix, bool erase)
{
	options_type::iterator i = options.lower_bound(prefix);
	if (i == options.end() || i->compare(0, prefix.size(), prefix) != 0)
		return std::string();
	std::string res = i->substr(prefix.size());
	if (erase)
		options.erase(i);
	return res;
}

bool cmdline::erase_option(const std::string & o)
{
	return options.erase(o) > 0;
}

void cmdline::erase_option_by_prefix(const std::string & prefix)
{
	options_type::iterator i = options.lower_bound(prefix);
	if (i == options.end() || i->compare(0, prefix.size(), prefix) != 0)
		return;
	options.erase(i);
}
